package com.weixiaodong.ModelDesign.适应设计模式.Adapter模式.练习;

import java.io.*;
import java.util.Properties;

/**
 * 具体的适配之后的对象
 */
@SuppressWarnings("all")
public class FileProperties implements FileIO {

    private Properties properties;

    public FileProperties(Properties properties) {
        this.properties = properties;
    }

    /**
     * 从指定文件中读取出属性键值对，然后将其封装到properties集合中
     * @param fileName
     * @throws IOException
     */
    @Override
    public void readFromFile(String fileName) throws IOException {
        Reader reader = null;

        try {
            reader = new InputStreamReader(new FileInputStream(fileName));

            properties.load(reader);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    @Override
    public void writeToFile(String fileName) throws IOException {
        Writer writer = null;

        try {
            writer = new OutputStreamWriter(new FileOutputStream(fileName));

            properties.store(writer, "write by FileProperties");
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    @Override
    public void setValue(String key, String value) {
        properties.setProperty(key, value);
    }

    @Override
    public void getValue(String key) {
        System.out.println(properties.get(key));
    }
}
